package com.wkr.leetcode;

import java.util.Arrays;

public class Sort {


    /**
     * 快排
     */
    public static void QKSourt(int[] a, int start, int end) {
        if (a.length < 0){
            return;
        }
        if (start >= end){
            return;
        }
        int left = start;
        int right = end;
        int temp = a[left];
        while (left < right){
            while (left < right && a[right] >= temp){
                right -- ;
            }
            a[left] = a[right];
            while (left < right && a[left] <= temp){
                left ++ ;
            }
            a[right] = a[left];
        }
        a[left] = temp;
        System.out.println(Arrays.toString(a));
        QKSourt(a, start, left -1);
        QKSourt(a,left+1,end);
    }

    public  void quick(int[] unsorted, int start, int end) {
        if (start >= end) {
            return;
        }
        int left = start;
        int right = end;
        int temp = unsorted[left];

        while (right > left) {
            while (left < right && unsorted[right] >= temp) {
                right--;
            }
            unsorted[left] = unsorted[right];
            while (left < right && unsorted[left] <= temp) {
                left++;
            }
            unsorted[right] = unsorted[left];
        }
        unsorted[left] = temp;
        quick(unsorted, start, left - 1);
        quick(unsorted, left + 1, end);

    }

    /**
     * 冒泡排序
     * 每一轮找出未排序前的最大值，交换到后面
     */
    public static void bubbleSort(int[] args) {
        for (int i = 0; i < args.length; i++) {
            for (int j = 0; j < args.length - i - 1; j++) {
                if (args[j] > args[j + 1]) {
                    int temp = args[j];
                    args[j] = args[j + 1];
                    args[j + 1] = temp;
                }
            }
        }
    }

    /**
     * 选择排序
     * 第一个开始 每一轮找出未排序前最小的数据 对比交换
     */
    public static void selectionSort(int[] args) {
        for (int i = 0; i < args.length; i++) {
            int min = args[i];
            int flag = i;
            for (int j = i + 1; j < args.length; j++) {
                if (args[j] < min) {
                    min = args[j];
                    flag = j;
                }
            }
            if (i != flag) {
                int temp = args[i];
                args[i] = args[flag];
                args[flag] = temp;
            }
        }
    }

    /**
     * 插入排序
     * 每一轮 前面一个作为已排序数据 后面一个作为未排序 与前面比较 后者小则交换
     */
    public static void insertSort(int[] args) {
        for (int i = 1; i < args.length; i++) {
            for (int j = i; j > 0; j--) {
                if (args[j] < args[j - 1]) {
                    int temp = args[j];
                    args[j] = args[j - 1];
                    args[j - 1] = temp;
                } else {
                    break;
                }
            }
        }
    }

    /**
     * 二分查找
     */
    public static int upper_bound(int n, int v, int[] a) {
        // write code here
        int left = 0;
        int right = n - 1;
        while (right >= left) {
            int mid = (left + right) / 2 + 1;
            if (a[mid] == v) {
                return mid + 1;
            } else if (a[mid] > v) {
                right = mid - 1;
            } else if (a[mid] < v) {
                left = mid + 1;
            }
        }
        return -1;
    }

}
